package com.sven.sharedb.core.utils;

import com.sven.sharedb.core.exception.BaseException;
import com.sven.sharedb.core.message.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * @author sven.wang
 * @version 2022/4/26 16:59
 */
public class CommonUtils {
    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(CommonUtils.class);

    /**
     * 调用rpc接口时，处理响应结果
     * @param response 响应结果
     */
    public static void dealResponse(BaseResponse response) {
        if(response ==null){
            return;
        }

        if(response.getStatus() != HttpStatus.OK.value())
        {
            LOG.error(response.getMessage());
            throw new BaseException(response.getMessage(),response.getStatus());
        }
    }

    /**
     * 判断请求返回值是不是200
     * @param response 响应结果
     */
    public static boolean isSuccess(BaseResponse response)
    {
        if(response == null){
            return false;
        }

        return response.getStatus() == HttpStatus.OK.value();
    }
}
